package com.app.command;

import java.io.BufferedReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.app.dao.BookDAO;
import com.app.dao.entity.Book;

public class CreateBookCommand extends Command {

	@Override
	protected CommandResult execute(BufferedReader dataLineReader)
			throws Exception {
		CommandResult result = new CommandResult(CommandResultStatus.SUCCESSFUL);
		BookDAO bookDAO = new BookDAO();

		System.out.print(" Enter Book Title, Book URL ");
		Pattern p = Pattern.compile("([a-zA-Z0-9]+),([a-zA-Z0-9]+)");
		Matcher m = p.matcher(dataLineReader.readLine());
		boolean b = m.matches();
		Book book = bookDAO.createBook(m.group(1), m.group(2));
		result.setMessage("Book created. Book Title: " + m.group(1)
				+ " Book URL: " + m.group(2));

		// String bookTitle = m.group(1);
		// String bookUrl = m.group(2);
		// Book book = bookDAO.createBook(bookTitle, bookUrl);
		// result.setMessage("Book created. Book Title: " + book.getBookTitle()
		// + " Book URL: " + book.getBookUrl());
		return result;

	}

}
